package com.surelution.projects.crm

class GasPriceService {

    static transactional = true

    def getStationGasPrice(GasStation station, GasType type) {
		def now = new Date()
		def stationGas = StationGas.findByStationAndGasType(station, type)
		if(stationGas) {
			def cri = Promoting.createCriteria()
			def promoting = cri.list() {
				eq("stationGas", stationGas)
				le("startDate", now)
				or{
					ge("endDate", now)
					isNull("endDate")
				}
			}
			println promoting
			if(promoting) {
				return promoting.price
			}
			
		}
		def bpCri = BoardPrice.createCriteria()
		def prices = bpCri.list() {
			eq("gasType", type)
			le("startDate", now)
			order("startDate", "desc")
		}
		if(prices && prices.size() > 0) {
			def price = prices[0]
			return price.price
		}
		return null
    }
}
